package com.xjtu.platform.demander.editquestion;


import com.xjtu.platform.common.CommonService;
import com.xjtu.platform.common.CurrentUser;
import com.xjtu.platform.common.Result;

import com.xjtu.platform.common.TokenUtils;
import com.xjtu.platform.respondent.convert.ConvertDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demander")
public class EditquestionAPI {

    @Resource
    private EditquestionService editquestionService;

    @Resource
    private CommonService commonService;

    @RequestMapping(value = "/editquestion",method = RequestMethod.GET)
    public Result convertList(@RequestHeader("Token") String token){
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        Map<String,Object> page = editquestionService.getEditquestionList(currentUser);
        return Result.success(page);
    }

    @PostMapping("/setoption")
    public Result setOptions(@RequestBody OptionsDto optionsDto){

        editquestionService.deleteoption(optionsDto);
        boolean ok = editquestionService.insertOption(optionsDto);
        if(ok){
            return Result.success("成功");
        }else {
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中。。。。。。");
        }
    }

    @PostMapping("/updatequestion")
    public Result updatequestion(@RequestBody EditquestionDto dto){
        try{
            editquestionService.updateQuestion(dto);
            return Result.success();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return Result.fail(400,"系统错误！");
    }

}
